package com.toe.sso.service.impl;

import com.toe.common.pojo.domain.ToeAuthenticationInfo;
import com.toe.common.utils.TimeUtil;
import com.toe.pojo.entity.user.UserPermission;
import com.toe.pojo.entity.user.UserUsertbl;
import com.toe.pojo.vo.user.UserAdminVO;
import com.toe.pojo.vo.user.UserBusinessVO;
import com.toe.pojo.vo.user.UserUserVO;
import com.toe.sso.mapper.AdminMapper;
import com.toe.sso.mapper.BusinessMapper;
import com.toe.sso.mapper.UserMapper;
import com.toe.sso.pojo.domain.AdminUserDetails;
import com.toe.sso.pojo.domain.BusinessUserDetails;
import com.toe.sso.pojo.domain.UserUserDetails;
import com.toe.sso.pojo.vo.UserInfoVO;
import com.toe.sso.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

// @DubboService注解表示当前业务逻辑层实现类中的所有方法
// 均会注册到Nacos,成为Dubbo可以被发现的业务逻辑层方法
@DubboService
@Service
@Slf4j
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public UserDetails userInfo() {

        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
//        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken=null;
//        if(authentication instanceof UsernamePasswordAuthenticationToken){
//            usernamePasswordAuthenticationToken=(UsernamePasswordAuthenticationToken)authentication;
//        }else{
//            return null;
//        }
//        //credentials是security框架中的存在上下文中的用户详情数据
//        Object credentials = usernamePasswordAuthenticationToken.getCredentials();
        ToeAuthenticationInfo authenticationInfo = (ToeAuthenticationInfo) credentials;
        if (authenticationInfo.getUserType().equals("ADMIN")){
            long adminId = authenticationInfo.getId();
            UserAdminVO adminVO = adminMapper.findById(adminId);
            AdminUserDetails adminUserDetails = new AdminUserDetails();
            BeanUtils.copyProperties(adminVO,adminUserDetails);
            List<String> authorities = authenticationInfo.getAuthorities();
            List<UserPermission> authoritiess = new ArrayList<>();
            for (String s:authorities
            ) {
                UserPermission authority=new UserPermission();
                authority.setPermissionValue(s);
                authoritiess.add(authority);
            }
            adminUserDetails.setAuthorities(authoritiess);
            return adminUserDetails;
        }else if (authenticationInfo.getUserType().equals("BUSINESS")){
            long businessId = authenticationInfo.getId();
            UserBusinessVO businessVO = businessMapper.findById(businessId);
            BusinessUserDetails businessUserDetails = new BusinessUserDetails();
            BeanUtils.copyProperties(businessVO,businessUserDetails);
            List<String> authorities = authenticationInfo.getAuthorities();
            List<UserPermission> authoritiess = new ArrayList<>();
            for (String s:authorities
            ) {
                UserPermission authority=new UserPermission();
                authority.setPermissionValue(s);
                authoritiess.add(authority);
            }
            businessUserDetails.setAuthorities(authoritiess);
            return businessUserDetails;
        }else{
            long userId = authenticationInfo.getId();
            UserUsertbl userUsertbl = userMapper.findById(userId);
            UserUserDetails userUserDetails = new UserUserDetails();
            BeanUtils.copyProperties(userUsertbl,userUserDetails);
            int age = (int) TimeUtil.getAge(userUsertbl.getBirthday());
            userUserDetails.setAge(age);
            List<String> authorities = authenticationInfo.getAuthorities();
            List<UserPermission> authoritiess = new ArrayList<>();
            for (String s:authorities
            ) {
                UserPermission authority=new UserPermission();
                authority.setPermissionValue(s);
                authoritiess.add(authority);
            }
            userUserDetails.setAuthorities(authoritiess);
            return userUserDetails;
        }
    }

    @Override
    public UserUserVO findUserById(long id) {
        UserUsertbl userUsertbl = userMapper.findById(id);
        UserUserVO userUserVO  = new UserUserVO();
        BeanUtils.copyProperties(userUsertbl,userUserVO);
        int age = (int) TimeUtil.getAge(userUsertbl.getBirthday());
        userUserVO.setAge(age);
        return userUserVO;
    }
}
